package com.test.test11;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.test.test11.activity.ControlActivity;
import com.test.test11.activity.DeviceActivity;
import com.test.test11.entity.UserDeviceRelationship;
import com.test.test11.http.AfterRequest;
import com.test.test11.task.DeviceDeleteTask;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备列表显示的适配器
 */

public class DeviceListAdapter extends BaseAdapter{

    private static final String TAG = "DEVICE_LLIST_ADAPTER";
    private LayoutInflater inflater;
    private List<UserDeviceRelationship> deviceList;
    private Context context;
    private DeviceActivity activity;

    public DeviceListAdapter(Context context,List<UserDeviceRelationship> userDeviceRelationships){
        System.out.println("hahahaah");
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.deviceList = userDeviceRelationships;
       // deviceList = SharedConstants.getDeviceList();
        if(deviceList==null){
            deviceList=new ArrayList<>();
        }
    }

    public void updateList(List<UserDeviceRelationship> list){
        this.deviceList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return deviceList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, final View convertView, ViewGroup parent) {
        System.out.println("87594378934885487");
        if(position>=deviceList.size()){
            return null;
        }
        final UserDeviceRelationship userDeviceRelationship = deviceList.get(position);
        View view = inflater.inflate(R.layout.device_list_item, null);
        TextView deviceNameTv = (TextView)view.findViewById(R.id.tv_device_name);
        deviceNameTv.setText(userDeviceRelationship.getRname());
        Button deviceControlBtn = (Button)view.findViewById(R.id.bt_device_control);
        Button deviceDeleteBtn = (Button)view.findViewById(R.id.bt_device_delete);

        deviceControlBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Config.getNETWORK()==0){
                    Toast.makeText(context, "请检查网络连接", Toast.LENGTH_LONG).show();
                    return;
                }
                Log.i(TAG, "点击了控制按钮："+userDeviceRelationship.getId());

                // 提示跳转到控制界面
                Intent intent = new Intent();
                intent.setClass(context,ControlActivity.class);
                context.startActivity(intent);
            }
        });

        deviceDeleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Config.getNETWORK()==0){
                    Toast.makeText(context, "请检查网络连接", Toast.LENGTH_LONG).show();
                    return;
                }
                DeviceDeleteTask task = new DeviceDeleteTask(new AfterRequest() {
                    @Override
                    public void success() {
                        Toast.makeText(context, "删除成功", Toast.LENGTH_LONG).show();
                        updateList(SharedConstants.getDeviceList());
                    }

                    @Override
                    public void failure(int code, String msg) {
                        if(code==-1) {
                            Toast.makeText(context, "请检查网络连接", Toast.LENGTH_LONG).show();
                        }else if(code==1001){
                            Toast.makeText(context, "删除失败，请重试", Toast.LENGTH_LONG).show();
                        }
                    }
                });

                task.execute(userDeviceRelationship);
            }
        });

        return view;
    }
}
